package com.yupi.yoj.model.dto.question;

import com.yupi.yoj.common.ErrorCode;
import com.yupi.yoj.exception.ThrowUtils;
import com.yupi.yoj.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.util.Date;

@Data
@Document(indexName = "question_v1")
public class QuestionEsDTO {

    @Id
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String title;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String content;

    @Field(type = FieldType.Keyword)
    private String tags;

    @Field(type = FieldType.Keyword)
    private String answer;

    @Field(type = FieldType.Integer)
    private Integer submitNum;

    @Field(type = FieldType.Integer)
    private Integer acceptedNum;

    @Field(type = FieldType.Keyword, index = false)
    private String judgeCase;

    @Field(type = FieldType.Keyword, index = false)
    private String judgeConfig;

    @Field(type = FieldType.Integer)
    private Integer thumbNum;

    @Field(type = FieldType.Integer)
    private Integer favourNum;

    @Field(type = FieldType.Integer)
    private Long userId;

    @Field(type = FieldType.Date)
    private Date createTime;

    @Field(type = FieldType.Date)
    private Date updateTime;

    @Field(type = FieldType.Keyword)
    private String isDelete;


    // Getters and setters

    public static QuestionEsDTO objToDto(Question question) {
        ThrowUtils.throwIf(question == null, ErrorCode.PARAMS_ERROR, "参数错误");
        QuestionEsDTO questionEsDTO = new QuestionEsDTO();
        BeanUtils.copyProperties(question, questionEsDTO);
        return questionEsDTO;
    }
}